package com.lishe.lishe.lishe.module.shopingcart.presenter;

import com.lishe.base.mvp.presenter.BaseMvpPresenter;
import com.lishe.lishe.lishe.bean.AddressBean;
import com.lishe.lishe.lishe.module.shopingcart.bean.ShopCarBean;
import com.lishe.lishe.lishe.module.shopingcart.bean.SubmitOrderBean;
import com.lishe.lishe.net.BaseProtocal;
import com.lishe.lishe.net.ResultListener;
import java.util.ArrayList;

/**
 * Bin
 */

public class ShopCarPresenter extends BaseMvpPresenter<ShopCarView> {

    private ShopCarModule module;

    public ShopCarPresenter() {
        module = new ShopCarModule();
    }

    public void getShopCarData() {
        if (getMvpView() != null) getMvpView().onLoading();
        module.getShopCarData(new ResultListener() {
            @Override
            public void onSuccess(BaseProtocal mBaseProtocal) {
                if (getMvpView() != null)
                    getMvpView().onInitSuccess((ShopCarBean) mBaseProtocal);
            }

            @Override
            public void onFail(int errcode, String msg) {
                if (getMvpView() != null) getMvpView().onRequestFailure(errcode, msg);
            }

            @Override
            public void onError(Throwable e) {
                if (getMvpView() != null) getMvpView().onRequestError();
            }
        });
    }

    public void getAddressData() {
        if (getMvpView() != null) getMvpView().onLoading();
        module.getAddressData(new ResultListener() {
            @Override
            public void onSuccess(BaseProtocal mBaseProtocal) {
                if (getMvpView() != null)
                    getMvpView().onAddressSuccess((AddressBean) mBaseProtocal);
            }

            @Override
            public void onFail(int errcode, String msg) {
                if (getMvpView() != null) getMvpView().onRequestFailure(errcode, msg);
            }

            @Override
            public void onError(Throwable e) {
                if (getMvpView() != null) getMvpView().onRequestError();
            }
        });
    }

    public void submitOrder(ArrayList<String> cartIds) {
        if (getMvpView() != null) getMvpView().onLoading();
        module.submitOrder(new ResultListener() {
            @Override
            public void onSuccess(BaseProtocal mBaseProtocal) {
                if (getMvpView() != null)
                    getMvpView().onSubmitSuccess((SubmitOrderBean) mBaseProtocal);
            }

            @Override
            public void onFail(int errcode, String msg) {
                if (getMvpView() != null) getMvpView().onRequestFailure(errcode, msg);
            }

            @Override
            public void onError(Throwable e) {
                if (getMvpView() != null) getMvpView().onRequestError();
            }
        }, cartIds);
    }

    public void modifyDefAddr(String addrId) {
        if (getMvpView() != null) getMvpView().onLoading();
        module.modifyDefAddr(new ResultListener() {
            @Override
            public void onSuccess(BaseProtocal mBaseProtocal) {
                if (getMvpView() != null)
                    getMvpView().onModifyDefAddrSuccess(mBaseProtocal);
            }

            @Override
            public void onFail(int errcode, String msg) {
                if (getMvpView() != null) getMvpView().onRequestFailure(errcode, msg);
            }

            @Override
            public void onError(Throwable e) {
                if (getMvpView() != null) getMvpView().onRequestError();
            }
        }, addrId);
    }

}
